package com.desaysv.hicar;

import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity2 extends AppCompatActivity {
    private static final String TAG = "[MainActivity2]";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e(TAG, "onCreate: ");
        setContentView(R.layout.activity_main2);

//        this.getWindow().getDecorView().postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                Intent intent = new Intent(MainActivity2.this, MainActivity.class);
//                startActivity(intent);
//            }
//        }, 5000L);

//        findViewById(R.id.tvSetting).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                //打开系统铃声设置
////                Intent intent = new Intent(
////                        RingtoneManager.ACTION_RINGTONE_PICKER);
////                intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT,
////                        false);
////                intent.putExtra(RingtoneManager.EXTRA_RINGTONE_INCLUDE_DRM,
////                        true);
////                // 设置铃声类型和title
////                intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE,
////                        RingtoneManager.TYPE_RINGTONE);
////                intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE,
////                        "linzi设置铃声");
////                // 当设置完毕之后返回到当前的Activity
////                startActivityForResult(intent, 10002);
//
//                // 打开系统铃声设置
//                Intent intent = new Intent(
//                        RingtoneManager.ACTION_RINGTONE_PICKER);
//                // 设置铃声类型和title
//                intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE,
//                        RingtoneManager.TYPE_RINGTONE);
//                intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE,
//                        "linzi设置铃声");
//                Uri ringtoneUri = RingtoneManager.getActualDefaultRingtoneUri(MainActivity2.this, RingtoneManager.TYPE_RINGTONE);
//                intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI,
//                        ringtoneUri);
//                // 当设置完毕之后返回到当前的Activity
//                startActivityForResult(intent, 10002);
//
//            }
//        });
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.e(TAG, "onRestart: ");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.e(TAG, "onPause: ");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.e(TAG, "onResume: ");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.e(TAG, "onStop: ");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.e(TAG, "onStart: ");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.e(TAG, "onDestroy: ");
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        Log.e(TAG, "onKeyDown: " + keyCode);
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        Log.e(TAG, "dispatchKeyEvent: " + event.getKeyCode());
        return super.dispatchKeyEvent(event);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 10002 && resultCode == RESULT_OK) {
            // 得到我们选择的铃声
            Uri pickedUri = data
                    .getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);

            // 将我们选择的铃声设置成为默认来电铃声
            RingtoneManager.setActualDefaultRingtoneUri(this,
//                    RingtoneManager.TYPE_RINGTONE, pickedUri);
                    RingtoneManager.TYPE_RINGTONE, pickedUri);
        }
    }
}
